﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;
using UFIDA.U9.Base.FlexField.DescFlexField;
using UFIDA.U9.Base.UOM;
using UFIDA.U9.CBO.FI.CFPDTO;
using UFIDA.U9.CBO.Pub.Controller;
using UFIDA.U9.CBO.Pub.Controller.Proxy;
using UFIDA.U9.CBO.PubBE.YYC;
using UFIDA.U9.CBO.SCM.Bin;
using UFIDA.U9.CBO.SCM.Item;
using UFIDA.U9.CBO.SCM.ProjectTask;
using UFIDA.U9.CBO.SCM.Supplier;
using UFIDA.U9.Cust.YYD.WebApi.Dtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.RequestDtos;
using UFIDA.U9.Cust.YYD.WebApi.Dtos.ReturnDtos;
using UFIDA.U9.Cust.YYD.WebApi.Public;
using UFIDA.UBF.SystemManage;
using UFIDA.UBF.SystemManage.Agent;
using UFSoft.UBF.AopFrame.Lock;
using UFSoft.UBF.Business;
using UFSoft.UBF.MVC;
using UFSoft.UBF.PL.Engine.Cache;
using UFSoft.UBF.PL;
using UFSoft.UBF.Transactions;
using UFSoft.UBF.Util.DataAccess;
using UFIDA.U9.PM.Rcv;
using UFIDA.U9.PM.ASN;
using UFIDA.U9.CBO.DTOs;
using UFIDA.U9.CBO.SCM.Warehouse;
using UFIDA.U9.Base;
using UFIDA.U9.PM.ASN.Proxy;
using UFIDA.U9.PM.Enums;
using UFIDA.U9.PM.DTOs;
using UFIDA.U9.Lot;
using System.Security.Cryptography;
using UFIDA.U9.PM.Rcv.Proxy;
using UFIDA.U9.ISV.RCV.Proxy;
using UFIDA.U9.ISV.RCV.DTO;
using UFSoft.UBF.MVC.Common;
using static UFSoft.UBF.MVC.Helper.ZipHelper;
using UFIDA.U9.ISV.PUB.RestSV.Model.Pub;
using ResultDTOData = UFIDA.U9.ISV.PUB.RestSV.Model.Pub.ResultDTOData;
using UFIDA.U9.Base.DTOs;
using UFIDA.U9.SPR.SalePriceAdjustment;
using UFIDA.U9.ISV.SM.Proxy;
using UFIDA.U9.SPR.SalePriceList;
using UFIDA.U9.PM.POChange;
using UFIDA.U9.SM.SOModify;
using UFIDA.U9.PR.PurchaseRequest;
using UFIDA.U9.CBO.SCM.Enums;
using System.Runtime.Remoting.Messaging;
using UFIDA.U9.ISV.PRSV;
using UFIDA.U9.ISV.PRSV.Proxy;
using UFIDA.U9.PM.PO;
using UFIDA.U9.SM.ForecastOrder;

namespace UFIDA.U9.Cust.YYD.WebApi.Controllers
{
    public class OAController : OpenAPIController
    {
        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitPriceAdjustment(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
                //接口逻辑
                ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

                List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
                ResultDTOData resultDTO = new ResultDTOData();
                List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
                foreach (OptDocRData data in datas)
                {
                    docDatas.Add(new IDCodeNameDTOData
                    {
                        ID = data.ID,
                        Code = data.Code
                    });
                }
                bool flag = docDatas == null || docDatas.Count == 0;
                if (flag)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = "传入业务数据为空，请确认";
                }
                foreach (IDCodeNameDTOData data2 in docDatas)
                {
                    resultDTO = new ResultDTOData();
                    resultDTO.IsSucess = false;
                    SalePriceAdjustment head = GetSalePriceAdjustment(data2);
                    bool flag2 = head == null;
                    if (flag2)
                    {
                        resultDTO.IsSucess = false;
                        resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                        resultDTO.ID = data2.ID.ToString();
                        resultDTO.Code = data2.Code;
                        resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = SPR.Enums.Status.Opened;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitSalePriceList(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                SalePriceList head = GetSalePriceList(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = SPR.Enums.Status.Opened;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitSOModify(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                SOModify head = GetSOModify(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = SM.SOModify.SOModifyStatusEnum.Openend;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitPR(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                UFIDA.U9.PR.PurchaseRequest.PR head = GetPR(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {

                            ApprovePRISVProxy p1 = new ApprovePRISVProxy();
                            p1.PRList = new List<PRConditionDTOData> { new PRConditionDTOData { 
                                PRID = head.ID,
                                DocNO = head.DocNo
                            } };
                            p1.ActionType = 8;
                            var rt = p1.Do();
                            if (rt.Count > 0)
                            {
                                if (!string.IsNullOrEmpty(rt[0].ErrMessage))
                                {
                                    throw new Exception(rt[0].ErrMessage);
                                }
                                else
                                {
                                    ApprovePRISVProxy p2 = new ApprovePRISVProxy();
                                    p2.PRList = new List<PRConditionDTOData> { new PRConditionDTOData {
                                        PRID = head.ID,
                                        DocNO = head.DocNo
                                    } };
                                    p2.ActionType = 9;
                                    var rt1 = p2.Do();
                                    if (rt1.Count > 0)
                                    {
                                        if (!string.IsNullOrEmpty(rt1[0].ErrMessage))
                                        {
                                            throw new Exception(rt1[0].ErrMessage);
                                        }
                                       
                                    }
                                    else
                                    {
                                        throw new Exception("反提交失败");
                                    }
                                }
                            }
                            else
                            {
                                throw new Exception("反提交失败");
                            }


                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }
        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitPurchaseOrder(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                PurchaseOrder head = GetPurchaseOrder(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = PM.PO.PODOCStatusEnum.Opened;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitSupplySource(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code,
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                SupplySource head = GetSupplySource(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = CBO.SCM.Supplier.SupplierSourceStatusEnum.OpenStatus;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        [HttpPost]
        public ApiResult<List<ResultDTOData>> UnSubmitForecastOrder(List<OptDocRData> datas)
        {
            //using (BPForEngine bp1 = new BPForEngine())
            //{
            //接口逻辑
            ApiResult<List<ResultDTOData>> rtn = new ApiResult<List<ResultDTOData>>();

            List<ResultDTOData> resultDTOs = new List<ResultDTOData>();
            ResultDTOData resultDTO = new ResultDTOData();
            List<IDCodeNameDTOData> docDatas = new List<IDCodeNameDTOData>();
            foreach (OptDocRData data in datas)
            {
                docDatas.Add(new IDCodeNameDTOData
                {
                    ID = data.ID,
                    Code = data.Code,
                });
            }
            bool flag = docDatas == null || docDatas.Count == 0;
            if (flag)
            {
                resultDTO.IsSucess = false;
                resultDTO.ErrorMsg = "传入业务数据为空，请确认";
            }
            foreach (IDCodeNameDTOData data2 in docDatas)
            {
                resultDTO = new ResultDTOData();
                resultDTO.IsSucess = false;
                ForecastOrder head = GetForecastOrder(data2);
                bool flag2 = head == null;
                if (flag2)
                {
                    resultDTO.IsSucess = false;
                    resultDTO.ErrorMsg = string.Format(UFIDA.U9.ISV.SM.RestSV.Res.SMRes.DocNotExistException01, data2.Code, data2.ID);
                    resultDTO.ID = data2.ID.ToString();
                    resultDTO.Code = data2.Code;
                    resultDTOs.Add(resultDTO);
                }
                else
                {
                    using (UBFTransactionScope scope = new UBFTransactionScope(TransactionOption.RequiresNew))
                    {
                        try
                        {
                            using (ISession session = Session.Open())
                            {
                                head.Status = ForecastOrderStatusEnum.Opened;
                                session.Commit();
                            }
                            scope.Commit();
                            resultDTO.IsSucess = true;
                            resultDTO.ID = data2.ID.ToString();
                            resultDTO.Code = data2.Code;
                        }
                        catch (Exception ex)
                        {
                            scope.Rollback();
                            resultDTO.IsSucess = false;
                            resultDTO.ErrorMsg = ex.Message;
                        }
                        resultDTOs.Add(resultDTO);
                    }
                }
            }
            rtn.Data = resultDTOs;
            return rtn;

            //}

        }

        public static SalePriceAdjustment GetSalePriceAdjustment(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            SalePriceAdjustment result;
            if (flag)
            {
                SalePriceAdjustment head = SalePriceAdjustment.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    SalePriceAdjustment head2 = SalePriceAdjustment.Finder.Find("DocNo=@docno and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("docno", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }

        public static SalePriceList GetSalePriceList(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            SalePriceList result;
            if (flag)
            {
                SalePriceList head = SalePriceList.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    SalePriceList head2 = SalePriceList.Finder.Find("Code=@Code and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("Code", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }

        public static SOModify GetSOModify(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            SOModify result;
            if (flag)
            {
                SOModify head = SOModify.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    SOModify head2 = SOModify.Finder.Find("DocNo=@docno and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("docno", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }

        public static UFIDA.U9.PR.PurchaseRequest.PR GetPR(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            UFIDA.U9.PR.PurchaseRequest.PR result;
            if (flag)
            {
                UFIDA.U9.PR.PurchaseRequest.PR head = UFIDA.U9.PR.PurchaseRequest.PR.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    UFIDA.U9.PR.PurchaseRequest.PR head2 = UFIDA.U9.PR.PurchaseRequest.PR.Finder.Find("DocNo=@docno and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("docno", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }


        public static SupplySource GetSupplySource(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            SupplySource result;
            if (flag)
            {
                SupplySource head = SupplySource.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    SupplySource head2 = SupplySource.Finder.Find("ItemInfo.ItemCode=@ItemCode and SupplierInfo.Name = @SupplierName and Org = @orgid", new OqlParam[]
                    {
                        new OqlParam("ItemCode", data.Code.Split('_')[0]),
                        new OqlParam("SupplierName", data.Code.Split('_')[1]),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }


        public static PurchaseOrder GetPurchaseOrder(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            PurchaseOrder result;
            if (flag)
            {
                PurchaseOrder head = PurchaseOrder.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    PurchaseOrder head2 = PurchaseOrder.Finder.Find("DocNo=@docno and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("docno", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }

        public static ForecastOrder GetForecastOrder(IDCodeNameDTOData data)
        {
            bool flag = data.ID > 0L;
            ForecastOrder result;
            if (flag)
            {
                ForecastOrder head = ForecastOrder.Finder.FindByID(data.ID);
                result = head;
            }
            else
            {
                bool flag2 = !string.IsNullOrEmpty(data.Code);
                if (flag2)
                {
                    ForecastOrder head2 = ForecastOrder.Finder.Find("DocNo=@docno and Org =@orgid", new OqlParam[]
                    {
                        new OqlParam("docno", data.Code),
                        new OqlParam("orgid", Context.LoginOrg.ID)
                    });
                    result = head2;
                }
                else
                {
                    result = null;
                }
            }
            return result;
        }
    }






}
